package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Generated;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;

/**
 * @auth 邝志豪
 * @date 2024/1/19 15:23
 */
@Controller
@RequestMapping(value = "/user")
public class SysUserController {
    private Logger log = Logger.getLogger(SysUserController.class);

    @RequestMapping(value = "/view")
    public String view() {
        log.info("查询用户详情");
        return null;
    }

    private ArrayList<SysUser> sysUsers = new ArrayList<SysUser>();

    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();

    public SysUserController() {
        try {
            sysUsers.add(new SysUser(5, "zhaojian", "赵静", "5555555",
                    1, "13054784445", "上海市宝山区", 1, 1, new Date(), 1, new Date()));

            sysUsers.add(new SysUser(4, "wanglin", "王林", "4444444",
                    1, "18965652364", "北京市学院路", 1, 1, new Date(), 1, new Date()));

            sysUsers.add(new SysUser(1, "test001", "测试用户001", "1111111",
                    1, "13566669998", "北京市朝阳区", 1, 1, new Date(), 1, new Date()));

            sysUsers.add(new SysUser(2, "zhaoyan", "赵燕", "2222222",
                    1, "18678786545", "北京市海淀区", 1, 1, new Date(), 1, new Date()));

            sysUsers.add(new SysUser(3, "test003", "测试用户003", "3333333",
                    1, "13121334531", "北京市海淀区", 1, 1, new Date(), 1, new Date()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询所有用户
    @GetMapping(value = "/list")
    public String list(Model model) {
        log.info("当查询条件为空时，查询用户信息");
        queryUserList.clear();
        queryUserList.addAll(sysUsers);
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }

    @PostMapping("/list")
    public String list(@RequestParam(value = "realName", required = false) String realName, Model model) {
        log.info("查询条件:【realName】=" + realName + ",查询用户信息");
        queryUserList.clear();
        if (realName != null && !realName.equals("")) {
            for (SysUser user : queryUserList) {
                if (user.getRealName().indexOf(realName) != -1) {
                    queryUserList.add(user);
                }
            }
        } else {
            queryUserList.addAll(sysUsers);
        }
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }
}

